Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gettotalsupply #1502

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Gettotalsupply #1502

wants to merge 9 commits into from

Conversation

levonpetrosyan93
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The changes introduce new functions and modify existing functionality in the src/rpc/misc.cpp file. Two new functions, getzerocoinpoolbalance and getCVE17144amount, are added to calculate specific balances related to the Zerocoin pool and a historical attack. The gettotalsupply function is updated to reflect these new calculations. Additionally, new RPC commands for querying these balances are registered, and the getinfoex function is enhanced to include total supply information.

Changes

File Change Summary
src/rpc/misc.cpp - Added functions: getzerocoinpoolbalance, getCVE17144amount
- Updated gettotalsupply to adjust total supply calculations
- Added RPC commands: getzerocoinpoolbalance, getCVE17144amount
- Modified getinfoex to include total supply in its response
- Maintained error handling in new functions

Poem

In the land of code where rabbits play,
New functions hop in, brightening the day.
Balances counted, both old and new,
With each little change, our joy just grew.
So here's to the tweaks, both clever and spry,
A hop and a skip, let our code fly high! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
src/rpc/misc.cpp (3)

1769-1771: Consider using range-based for loops for improved readability

The iterator-based loops can be simplified using range-based for loops, which enhance readability and reduce the potential for errors.

Apply this change:

-for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) {
-    nTotalAmount += it->second;
+for (const auto& entry : addressIndex) {
+    nTotalAmount += entry.second;
}
🧰 Tools
🪛 cppcheck (2.10-2)

[performance] 1769-1769: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)


1769-1769: Prefer prefix increment operator for iterators

Using the prefix increment operator (++it) is more efficient for iterators of non-primitive types because it avoids unnecessary copies.

Apply this minor change:

-for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) {
+for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); ++it) {
🧰 Tools
🪛 cppcheck (2.10-2)

[performance] 1769-1769: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)


1803-1805: Avoid variable shadowing by renaming the inner tx variable

The variable tx is redeclared within the inner scope, shadowing the outer tx and potentially causing confusion.

Rename the inner variable to prevent shadowing:

-CTransactionRef tx;
+CTransactionRef prevTx;
 uint256 hashBlock;
-if (!GetTransaction(txin.prevout.hash, tx, Params().GetConsensus(), hashBlock, true)) {
+if (!GetTransaction(txin.prevout.hash, prevTx, Params().GetConsensus(), hashBlock, true)) {
     continue;
 }
-amount += tx->vout[txin.prevout.n].nValue;
+amount += prevTx->vout[txin.prevout.n].nValue;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6da96d8 and 5019320.

📒 Files selected for processing (1)
  • src/rpc/misc.cpp (3 hunks)
🧰 Additional context used
🪛 cppcheck (2.10-2)
src/rpc/misc.cpp

[performance] 1769-1769: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)


[performance] 1778-1778: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)

🔇 Additional comments (4)
src/rpc/misc.cpp (4)

1777-1780: Consider using range-based for loops for improved readability

Similar to the previous loop, you can simplify this loop using a range-based for loop.

🧰 Tools
🪛 cppcheck (2.10-2)

[performance] 1778-1778: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)


1778-1778: Prefer prefix increment operator for iterators

Same as above, using ++it is more efficient.

🧰 Tools
🪛 cppcheck (2.10-2)

[performance] 1778-1778: Prefer prefix ++/-- operators for non-primitive types.

(postfixOperator)


1865-1882: Rename overloaded functions to avoid confusion with different return types

Similarly, the function getCVE17144amount() is overloaded with different return types. Renaming one of them will prevent confusion.


1797-1798: ⚠️ Potential issue

Variable vInOutPoints is redeclared, causing shadowing and logical errors

The variable vInOutPoints is declared twice: once outside the if block and once inside it, which leads to the inner declaration shadowing the outer one. This causes the outer vInOutPoints to remain unused and may lead to incorrect behavior.

Remove the inner declaration to use the intended vInOutPoints:

-            std::set<COutPoint> vInOutPoints;

Likely invalid or redundant comment.

src/rpc/misc.cpp Outdated Show resolved Hide resolved
src/rpc/misc.cpp Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5019320 and 401a88f.

📒 Files selected for processing (1)
  • src/rpc/misc.cpp (3 hunks)
🔇 Additional comments (1)
src/rpc/misc.cpp (1)

2017-2018: LGTM!

The RPC command registration follows the existing pattern and correctly maps the new functions.

src/rpc/misc.cpp Show resolved Hide resolved
src/rpc/misc.cpp Show resolved Hide resolved
src/rpc/misc.cpp Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/rpc/misc.cpp (1)

2020-2021: Rename overloaded functions to avoid confusion with different return types

The function getzerocoinpoolbalance() is overloaded with different return types (CAmount and UniValue), which can cause confusion and potential errors.

Consider renaming one of the functions to clarify their purposes. For example:

  • Rename the RPC handler to getzerocoinpoolbalanceRPC().
  • Update the command registration accordingly.
-UniValue getzerocoinpoolbalance(const JSONRPCRequest& request)
+UniValue getzerocoinpoolbalanceRPC(const JSONRPCRequest& request)

...

-{ "addressindex", "getzerocoinpoolbalance", &getzerocoinpoolbalance, false },
+{ "addressindex", "getzerocoinpoolbalance", &getzerocoinpoolbalanceRPC, false },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 401a88f and 76bc9ec.

📒 Files selected for processing (1)
  • src/rpc/misc.cpp (3 hunks)
🔇 Additional comments (3)
src/rpc/misc.cpp (3)

1785-1825: LGTM! Previous review comments have been addressed

The implementation now includes:

  • Proper chain height validation
  • Error handling for block reading
  • Documentation explaining CVE-2018-17144
  • Bounds checking for output index

1848-1850: 🛠️ Refactor suggestion

Document magic numbers and add validation

The total supply calculation uses magic numbers without proper documentation and lacks validation of the adjustment values.

Apply this diff to improve the code:

+    // Adjust total supply by:
+    // 1. Subtracting zerocoin pool balance (coins forged during Zerocoin attacks)
+    CAmount zeroPoolBalance = getzerocoinpoolbalance();
+    if (zeroPoolBalance == 0) {
+        throw std::runtime_error("Failed to calculate zerocoin pool balance");
+    }
+    total -= zeroPoolBalance;
+
+    // 2. Adding coins forged during CVE-2018-17144 attacks
+    CAmount cveAmount = getCVE17144amount();
+    if (cveAmount == 0) {
+        throw std::runtime_error("Failed to calculate CVE-17144 amount");
+    }
+    total += cveAmount;
+
+    // 3. Subtracting coins sent to unrecoverable address
+    // Reference: https://explorer.firo.org/tx/0b53178c1b22bae4c04ef943ee6d6d30f2483327fe9beb54952951592e8ce368
+    const CAmount BURNT_COINS = 16810168037465;
+    total -= BURNT_COINS;

Likely invalid or redundant comment.


1762-1783: ⚠️ Potential issue

Fix balance calculation and add error handling

The current implementation has potential issues:

  1. The balance calculation adds both mints and spends without considering signs, which could lead to incorrect totals.
  2. Missing error handling for GetAddressIndex failures.

Apply this diff to fix the calculation and add error handling:

 CAmount getzerocoinpoolbalance()
 {
     CAmount nTotalAmount = 0;
 
     // Iterate over all mints
     std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex;
-    if (GetAddressIndex(uint160(), AddressType::zerocoinMint, addressIndex)) {
+    if (!GetAddressIndex(uint160(), AddressType::zerocoinMint, addressIndex)) {
+        throw std::runtime_error("Failed to get zerocoin mint index");
+    }
     for (auto& it : addressIndex) {
         nTotalAmount += it.second;
     }
     addressIndex.clear();
 
     // Iterate over all spends
-    if (GetAddressIndex(uint160(), AddressType::zerocoinSpend, addressIndex)) {
+    if (!GetAddressIndex(uint160(), AddressType::zerocoinSpend, addressIndex)) {
+        throw std::runtime_error("Failed to get zerocoin spend index");
+    }
     for (auto& it : addressIndex) {
-        nTotalAmount += it.second;
+        nTotalAmount -= it.second;  // Subtract spends
     }
 
     return nTotalAmount;
 }

Likely invalid or redundant comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant